package com.zzptc.zzsgz.bdsjws.fragments;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragment;
import com.zzptc.zzsgz.bdsjws.services.MyDownLoadService;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/16.
 */

public class UpdataVersionFragment extends BaseFragment {
    @BindView(R.id.tv_updata)
    TextView tvUpdata;
    Unbinder unbinder;
    VersionInfo version1;
    @BindView(R.id.btn_yes)
    Button btnYes;
    @BindView(R.id.btn_no)
    Button btnNo;
    Unbinder unbinder1;

    public String getMsg() {
        Bundle bundle = getArguments();

        return bundle.getString("msg");
    }

    @Override
    public void initData(View view) {
        //更新TextView  getArgument
        Bundle bundle = getArguments();
        //VersionInfo versionInfo= (VersionInfo) bundle.get("versionInfo");
        //tvUpdata.setText(versionInfo.getInfo());
        version1 = (VersionInfo) bundle.getSerializable("versionInfo");
        btnNo.setVisibility(View.VISIBLE);
        if (version1 != null) {
            tvUpdata.setText(version1.getInfo());

        } else {
            tvUpdata.setText(getMsg());
        }

    }

    @Override
    public int getContentXml() {
        return R.layout.updata_version;
    }

    //写一个方法构造UpdataVersionFragment对象
    public static UpdataVersionFragment getUpdataVersionFragment(VersionInfo versionInfo) {
        UpdataVersionFragment updataVersionFragment = new UpdataVersionFragment();
        Bundle bundle = new Bundle();
        String aplurl=versionInfo.getApkurl();
        bundle.putSerializable("versionInfo", versionInfo);
        updataVersionFragment.setArguments(bundle);//设置值
        return updataVersionFragment;
    }

    //写一个方法构造UpdataVersionFragment对象 方法的重载
    public static UpdataVersionFragment getUpdataVersionFragment(String mag) {
        UpdataVersionFragment updataVersionFragment = new UpdataVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg", mag);
        updataVersionFragment.setArguments(bundle);//设置值
        return updataVersionFragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder!=null) {
            unbinder.unbind();
        }
    }
    //创建通知栏
    public static void createNotification(){
        NotificationManager notificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder notificationBuild = new NotificationCompat.Builder(MyApplication.getContext());
        notificationBuild.setContentTitle("下载中……")
                .setContentText("准备下载……")
                .setSmallIcon(R.mipmap.logo_small)
                .setOngoing(true) //设置他为一个正在进行的通知
                .setAutoCancel(false);
        notificationManager.notify(0, notificationBuild.build());


    }
    @OnClick({R.id.btn_yes, R.id.btn_no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_yes:

                Intent intent=new Intent();
                intent.setClass(getActivity(), MyDownLoadService.class);
                //传字符串过去
                //intent.putExtra("apkurl",version1.getApkurl());
                getActivity().startService(intent);


                dismiss();
                break;
            case R.id.btn_no:

                dismiss();
                break;
        }
    }
}
